package com.box.controller;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.box.entity.BlogSearch;
import com.box.entity.BlogTypeEntity;
import com.box.entity.DbAdmin;
import com.box.entity.DbBlog;
import com.box.entity.DbBtype;
import com.box.entity.SelBlogAndType;
import com.box.service.AdminService;
import com.box.service.BlogService;
import com.box.service.BlogTypeService;
import com.box.utils.BaiduPage;
import com.box.utils.JsonUtils;
import com.box.utils.LayuiResult;
import com.box.utils.PageView;

import com.box.utils.SessionUtils;


import java.io.PrintWriter;


@Controller
@RequestMapping("/blog")
public class BlogController { 
	
	@Autowired
	private BlogService blogService;
	
	@Autowired
	private AdminService adminService;
	
	@Autowired
	private BlogTypeService BTypeService;
	

	
	@Value("${TYPEID}")
	private String TYPEID;

	private PrintWriter out;
	
	@RequestMapping(value="/BlogList",method={RequestMethod.GET,RequestMethod.POST})
	@ResponseBody
	public LayuiResult selBlogList(Integer page,Integer limit,BlogSearch search){
	
		LayuiResult selBlogList = blogService.selBlogList(page, limit, search);
		
		return selBlogList;
	}
	
	@RequestMapping(value="/Vue/heartBlogList",method={RequestMethod.GET,RequestMethod.POST})
	@ResponseBody
	public LayuiResult selBlogListByVueAboutBlogList(HttpServletResponse response){
		 response.setHeader("Access-Control-Allow-Origin", "*");
		SelBlogAndType heartNote = blogService. selBlogTypeByTypeId(0);
		List<DbBlog> heartBlogList = heartNote.getBlog(); 
		LayuiResult result=new LayuiResult();
		try {
			result.setCode(0);
			result.setCount((long) heartBlogList.size());
			result.setMsg("获取博客心路历程成功");
			result.setData(heartBlogList);
			return result;
		} catch (Exception e) {
			// TODO: handle exception
			result.setCode(500);
			result.setCount(0l);
			result.setMsg("获取博客心路历程失败");
			result.setData(e);
			return result;
		}
		
	
	}
	
	@RequestMapping(value="/Vue/BlogList",method={RequestMethod.GET,RequestMethod.POST})
	@ResponseBody
	public LayuiResult selBlogListByVue(HttpServletResponse response){
		 response.setHeader("Access-Control-Allow-Origin", "*");
		LayuiResult result=new LayuiResult();
	 List<DbBlog> list = blogService.selBlogList();
	 int size = list.size();
	 
	 try {
		 result.setCode(0);
		 result.setMsg("vue前端获取博客列表成功");
		 result.setCount((long) size);
		 result.setData(list);
		 return result;
	} catch (Exception e) {
		 result.setCode(500);
		 result.setMsg("vue前端获取博客列表失败"+e);
		 result.setCount(0l);
		 result.setData(e);
		 return result;
	}
	
	}
	
	@RequestMapping(value="/delBlogById/{id}",method={RequestMethod.GET,RequestMethod.POST})
	@ResponseBody
	public LayuiResult delBlogById(@PathVariable("id") String id) throws Exception{
	
		Integer n = blogService.delBlogById(Integer.parseInt(id));
		if(n>0){


			
			
		//删除对应索引
			return new LayuiResult(0,"单个删除成功");
		}else{
			return new LayuiResult(500,"单个删除失败");
		}
		
	}
	//修改博客内容
	@RequestMapping(value="/UpdateBlog",method={RequestMethod.GET,RequestMethod.POST})
	@ResponseBody
	public LayuiResult updateBlogById(DbBlog blog) throws Exception{
	
		Integer n = blogService.UpdateBlog(blog);
		if(n==1){

			return new LayuiResult(0,"修改成功");
		}else{
			return new LayuiResult(500,"修改失败");
		}
		
	}
	//添加内容
	@RequestMapping(value="/InsertBlog",method={RequestMethod.GET,RequestMethod.POST})
	@ResponseBody
	public LayuiResult InsertBy(DbBlog blog) throws Exception{

		Integer n = blogService.InsertBlog(blog);
		if(n==1){

			return new LayuiResult(0,"添加成功");
		}else{
			return new LayuiResult(500,"添加失败");
		}
		
	}
	
	//批量删除
		@RequestMapping(value="/delBlog/{BlogStr}",method={RequestMethod.GET,RequestMethod.POST})
		@ResponseBody
		public LayuiResult DeleteByAll(@PathVariable("BlogStr") String BlogStr) throws Exception{
		
			Integer n = blogService.delBlogByMany(BlogStr);
			if(n==1){
				return new LayuiResult(0,"批量删除成功");
			}else{
				return new LayuiResult(500,"批量删除失败");
			}
			
		}
		
		//批量修改为已审核
				@RequestMapping(value="/updateBlogListByNotUppidAll/{blogStr}",method={RequestMethod.GET,RequestMethod.POST})
				@ResponseBody
				public LayuiResult updateBlogListByNotUppidAll(@PathVariable("blogStr") String blogStr){
				
					Integer n = blogService.updateBlogByUppid(blogStr);
					if(n==1){
						return new LayuiResult(0,"批量修改状态成功");
					}else{
						return new LayuiResult(500,"批量修改状态失败");
					}
					
				}
				
				//批量修改为待审核
				@RequestMapping(value="/updateBlogListByUppidAll/{blogStr}",method={RequestMethod.GET,RequestMethod.POST})
				@ResponseBody
				public LayuiResult updateBlogListByUppidAll(@PathVariable("blogStr") String blogStr){
				
					Integer n = blogService.updateBlogByNotUppid(blogStr);
					if(n==1){
						return new LayuiResult(0,"批量修改状态成功");
					}else{
						return new LayuiResult(500,"批量修改状态失败");
					}
					
				}
		//前端页面分页
		@RequestMapping(value="/personBlogList",method={RequestMethod.GET,RequestMethod.POST})
		@ResponseBody
		public String baiduPage(String page,HttpServletResponse response){
		
			
			System.out.println(page+"..............ajax的pagew");
			//博客类型ID=1个人博客日记
		 String TypeId = SessionUtils.getSessionAttribute(TYPEID).toString();
			 BaiduPage b = blogService.selBlogListByTypeId(Integer.parseInt(TypeId), Integer.parseInt(page));
			
			 if(b!=null){
				
				return JsonUtils.objectToJson2(b.getPageView(),response,b.getListType());
			}else{
				return JsonUtils.objectToJson(404);
			}
			
		}
		
		
				//首页页面分页
				@RequestMapping(value="/indexBlogList",method={RequestMethod.GET,RequestMethod.POST})
				@ResponseBody
				public String indexBlogList(String page,HttpServletResponse response){
				
					
					System.out.println(page+"..............index的pagew");
					 PageView b = blogService.selBlogListByIndexAjax(Integer.parseInt(page));
					
					 if(b!=null){
						
						return JsonUtils.objectToJson2(b.getBaidu(),response,b.getListType());
					}else{
						return JsonUtils.objectToJson(404);
					}
					
				}
				
				
				//增加点赞人数
				@RequestMapping(value="/addlikeId",method={RequestMethod.GET,RequestMethod.POST})
				@ResponseBody
				public String addlikeId(String id,HttpServletResponse response) throws Exception{
				
					
				System.out.println(id+"........................博客内容页面点赞功能成功实现.................");
					Integer n = blogService.updateLikeId(Integer.parseInt(id));
				
					if(n>0){
			
						Integer count = blogService.selLikeIdById(Integer.parseInt(id));
						return	JsonUtils.objectToJson3(count, response);
						
					}else{

						return	JsonUtils.objectToJson(404);
					}
					
					
				}
				
				
				//未审核列表
				@RequestMapping(value="/BlogListByNotUppid",method={RequestMethod.GET,RequestMethod.POST})
				@ResponseBody
				public LayuiResult BlogListByNotUppid(Integer page,Integer limit) {
				
					return blogService.selBlogByNotUppid(page, limit);
					
					
				}
				//已审核列表
				@RequestMapping(value="/BlogListByUppid",method={RequestMethod.GET,RequestMethod.POST})
				@ResponseBody
				public LayuiResult BlogListByUppid(Integer page,Integer limit) {
				
					return blogService.selBlogByUppid(page, limit);
					
					
				}
				
				//未审核列表提交进行审核
				@RequestMapping(value="/ClickBlogListByNotUppid/{id}",method={RequestMethod.GET,RequestMethod.POST})
				@ResponseBody
				public LayuiResult updateBlogStatusByUppid(@PathVariable("id") String id) {
				
						 Integer n= blogService.updateBlogStatusByUppid(Integer.parseInt(id));
						 if(n>0){
							 return new LayuiResult(0, "修改成功");
						 }else{
							 return new LayuiResult(500,"修改失败");
						 }
					
				}
			//未审核列表提交进行审核
				@RequestMapping(value="/ClickByUppid/{id}",method={RequestMethod.GET,RequestMethod.POST})
				@ResponseBody
				public LayuiResult updateBlogStatusByNotUppid(@PathVariable("id") String id) {
				
					 Integer n= blogService.updateBlogStatusByNotUppid(Integer.parseInt(id));
					 if(n>0){
						 return new LayuiResult(0, "驳回成功");
					 }else{
						 return new LayuiResult(500,"驳回失败");
					 }
					
					
				}
				
				//批量删除
				@RequestMapping(value="/delBlogListByNotUppidAll/{blogStr}",method={RequestMethod.GET,RequestMethod.POST})
				@ResponseBody
				public LayuiResult delBlogListByNotUppidAll(@PathVariable("blogStr") String blogStr) throws Exception{
				
					Integer n = blogService.delBlogByMany(blogStr);
					if(n>0){
						return new LayuiResult(0,"批量删除成功");
					}else{
						return new LayuiResult(500,"批量删除失败");
					}
					
				}
				
				
				
/**
 * ========================页面跳转======================
 * 
 * 
 */
	@RequestMapping(value="/page/BlogList",method={RequestMethod.GET,RequestMethod.POST})
	public String showBlogList(Model model){
		
		try {
			DbAdmin admin = adminService.getDbAdminItems();
			model.addAttribute("admin", admin);
			List<DbBtype> type = BTypeService.selBlogTypeList();
			model.addAttribute("type", type);
			return "blog/BlogList";
		} catch (Exception e) {
			// TODO: handle exception
			String error="您好，您的会话时期已过期，请重新登陆！";
			model.addAttribute("error", error);
			return "errpr/404";
		}
		

	}
	
	//去修改页面
	@RequestMapping(value="/page/EditBlog/{id}",method={RequestMethod.GET,RequestMethod.POST})
	public String showEditBlogPage(@PathVariable("id") String id,Model model){
		DbAdmin admin = adminService.getDbAdminItems();
		model.addAttribute("admin", admin);
		BlogTypeEntity selBlogById = blogService.selBlogById(Integer.parseInt(id));
		model.addAttribute("blog", selBlogById);
		Integer typeid = selBlogById.getTypeid();
		DbBtype selBlogTypeById = BTypeService.selBlogTypeById(typeid);
		model.addAttribute("type", selBlogTypeById);
		List<DbBtype> types = BTypeService.selBlogTypeList();
		model.addAttribute("types", types);
		return "blog/EditBlog";
	}
	
	//去添加页面
	@RequestMapping(value="/page/AddBlog",method={RequestMethod.GET,RequestMethod.POST})
	public String showAddBlogPage(Model model){
		DbAdmin admin = adminService.getDbAdminItems();
		model.addAttribute("admin", admin);	
		List<DbBtype> types = BTypeService.selBlogTypeList();
		model.addAttribute("BlogType", types);
		return "blog/AddBlog";
	}
	
	//博客未审核列表
	@RequestMapping(value="/page/BlogListByNotUppid",method={RequestMethod.GET,RequestMethod.POST})
	public String showBlogListByNotUppid(Model model){
		DbAdmin admin = adminService.getDbAdminItems();
		model.addAttribute("admin", admin);	
		
		return "blog/BlogByNotUppid";
	}
	
	//博客已审核列表
		@RequestMapping(value="/page/BlogListByUppid",method={RequestMethod.GET,RequestMethod.POST})
		public String showBlogListByUppid(Model model){
			DbAdmin admin = adminService.getDbAdminItems();
			model.addAttribute("admin", admin);	
			
			return "blog/BlogByUppid";
		}
	
}
